package f.f0.i;

import f.q;
import g.a0;
import g.e;
import g.y;
import g.z;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Objects;
import javax.annotation.Nullable;

/* compiled from: Http2Stream */
public final class p {
    public long a = 0;
    public long b;
    public final int c;
    public final g d;
    public final Deque<q> e;
    public boolean f;
    public final b g;
    public final a h;
    public final c i;
    public final c j;
    public b k;

    /* compiled from: Http2Stream */
    public final class a implements y {
        public final e a = new e();
        public boolean b;
        public boolean c;

        public a0 B() {
            return p.this.j;
        }

        public void E(e eVar, long j) throws IOException {
            this.a.E(eVar, j);
            while (this.a.b >= 16384) {
                a(false);
            }
        }

        /* JADX WARNING: Missing block: B:16:?, code skipped:
            r1.j.n();
            r11.d.b();
            r9 = java.lang.Math.min(r11.d.b, r11.a.b);
            r1 = r11.d;
            r1.b -= r9;
     */
        public final void a(boolean r12) throws java.io.IOException {
            /*
            r11 = this;
            r0 = f.f0.i.p.this;
            monitor-enter(r0);
            r1 = f.f0.i.p.this;	 Catch:{ all -> 0x007b }
            r1 = r1.j;	 Catch:{ all -> 0x007b }
            r1.i();	 Catch:{ all -> 0x007b }
        L_0x000a:
            r1 = f.f0.i.p.this;	 Catch:{ all -> 0x0072 }
            r2 = r1.b;	 Catch:{ all -> 0x0072 }
            r4 = 0;
            r6 = (r2 > r4 ? 1 : (r2 == r4 ? 0 : -1));
            if (r6 > 0) goto L_0x0024;
        L_0x0014:
            r2 = r11.c;	 Catch:{ all -> 0x0072 }
            if (r2 != 0) goto L_0x0024;
        L_0x0018:
            r2 = r11.b;	 Catch:{ all -> 0x0072 }
            if (r2 != 0) goto L_0x0024;
        L_0x001c:
            r2 = r1.k;	 Catch:{ all -> 0x0072 }
            if (r2 != 0) goto L_0x0024;
        L_0x0020:
            r1.j();	 Catch:{ all -> 0x0072 }
            goto L_0x000a;
        L_0x0024:
            r1 = r1.j;	 Catch:{ all -> 0x007b }
            r1.n();	 Catch:{ all -> 0x007b }
            r1 = f.f0.i.p.this;	 Catch:{ all -> 0x007b }
            r1.b();	 Catch:{ all -> 0x007b }
            r1 = f.f0.i.p.this;	 Catch:{ all -> 0x007b }
            r1 = r1.b;	 Catch:{ all -> 0x007b }
            r3 = r11.a;	 Catch:{ all -> 0x007b }
            r3 = r3.b;	 Catch:{ all -> 0x007b }
            r9 = java.lang.Math.min(r1, r3);	 Catch:{ all -> 0x007b }
            r1 = f.f0.i.p.this;	 Catch:{ all -> 0x007b }
            r2 = r1.b;	 Catch:{ all -> 0x007b }
            r2 = r2 - r9;
            r1.b = r2;	 Catch:{ all -> 0x007b }
            monitor-exit(r0);	 Catch:{ all -> 0x007b }
            r0 = r1.j;
            r0.i();
            r0 = f.f0.i.p.this;	 Catch:{ all -> 0x0069 }
            r5 = r0.d;	 Catch:{ all -> 0x0069 }
            r6 = r0.c;	 Catch:{ all -> 0x0069 }
            if (r12 == 0) goto L_0x005a;
        L_0x004f:
            r12 = r11.a;	 Catch:{ all -> 0x0069 }
            r0 = r12.b;	 Catch:{ all -> 0x0069 }
            r12 = (r9 > r0 ? 1 : (r9 == r0 ? 0 : -1));
            if (r12 != 0) goto L_0x005a;
        L_0x0057:
            r12 = 1;
            r7 = 1;
            goto L_0x005c;
        L_0x005a:
            r12 = 0;
            r7 = 0;
        L_0x005c:
            r8 = r11.a;	 Catch:{ all -> 0x0069 }
            r5.v(r6, r7, r8, r9);	 Catch:{ all -> 0x0069 }
            r12 = f.f0.i.p.this;
            r12 = r12.j;
            r12.n();
            return;
        L_0x0069:
            r12 = move-exception;
            r0 = f.f0.i.p.this;
            r0 = r0.j;
            r0.n();
            throw r12;
        L_0x0072:
            r12 = move-exception;
            r1 = f.f0.i.p.this;	 Catch:{ all -> 0x007b }
            r1 = r1.j;	 Catch:{ all -> 0x007b }
            r1.n();	 Catch:{ all -> 0x007b }
            throw r12;	 Catch:{ all -> 0x007b }
        L_0x007b:
            r12 = move-exception;
            monitor-exit(r0);	 Catch:{ all -> 0x007b }
            throw r12;
            */
            throw new UnsupportedOperationException("Method not decompiled: f.f0.i.p$a.a(boolean):void");
        }

        /* JADX WARNING: Missing block: B:8:0x000a, code skipped:
            r0 = r13.d;
     */
        /* JADX WARNING: Missing block: B:9:0x0011, code skipped:
            if (r0.h.c != false) goto L_0x0034;
     */
        /* JADX WARNING: Missing block: B:11:0x001b, code skipped:
            if (r13.a.b <= 0) goto L_0x0029;
     */
        /* JADX WARNING: Missing block: B:13:0x0023, code skipped:
            if (r13.a.b <= 0) goto L_0x0034;
     */
        /* JADX WARNING: Missing block: B:14:0x0025, code skipped:
            a(true);
     */
        /* JADX WARNING: Missing block: B:15:0x0029, code skipped:
            r0.d.v(r0.c, true, null, 0);
     */
        /* JADX WARNING: Missing block: B:16:0x0034, code skipped:
            r1 = r13.d;
     */
        /* JADX WARNING: Missing block: B:17:0x0036, code skipped:
            monitor-enter(r1);
     */
        /* JADX WARNING: Missing block: B:19:?, code skipped:
            r13.b = true;
     */
        /* JADX WARNING: Missing block: B:20:0x0039, code skipped:
            monitor-exit(r1);
     */
        /* JADX WARNING: Missing block: B:21:0x003a, code skipped:
            r13.d.d.r.flush();
            r13.d.a();
     */
        /* JADX WARNING: Missing block: B:22:0x0048, code skipped:
            return;
     */
        public void close() throws java.io.IOException {
            /*
            r13 = this;
            r0 = f.f0.i.p.this;
            monitor-enter(r0);
            r1 = r13.b;	 Catch:{ all -> 0x004c }
            if (r1 == 0) goto L_0x0009;
        L_0x0007:
            monitor-exit(r0);	 Catch:{ all -> 0x004c }
            return;
        L_0x0009:
            monitor-exit(r0);	 Catch:{ all -> 0x004c }
            r0 = f.f0.i.p.this;
            r1 = r0.h;
            r1 = r1.c;
            r2 = 1;
            if (r1 != 0) goto L_0x0034;
        L_0x0013:
            r1 = r13.a;
            r3 = r1.b;
            r5 = 0;
            r1 = (r3 > r5 ? 1 : (r3 == r5 ? 0 : -1));
            if (r1 <= 0) goto L_0x0029;
        L_0x001d:
            r0 = r13.a;
            r0 = r0.b;
            r3 = (r0 > r5 ? 1 : (r0 == r5 ? 0 : -1));
            if (r3 <= 0) goto L_0x0034;
        L_0x0025:
            r13.a(r2);
            goto L_0x001d;
        L_0x0029:
            r7 = r0.d;
            r8 = r0.c;
            r9 = 1;
            r10 = 0;
            r11 = 0;
            r7.v(r8, r9, r10, r11);
        L_0x0034:
            r1 = f.f0.i.p.this;
            monitor-enter(r1);
            r13.b = r2;	 Catch:{ all -> 0x0049 }
            monitor-exit(r1);	 Catch:{ all -> 0x0049 }
            r0 = f.f0.i.p.this;
            r0 = r0.d;
            r0 = r0.r;
            r0.flush();
            r0 = f.f0.i.p.this;
            r0.a();
            return;
        L_0x0049:
            r0 = move-exception;
            monitor-exit(r1);	 Catch:{ all -> 0x0049 }
            throw r0;
        L_0x004c:
            r1 = move-exception;
            monitor-exit(r0);	 Catch:{ all -> 0x004c }
            throw r1;
            */
            throw new UnsupportedOperationException("Method not decompiled: f.f0.i.p$a.close():void");
        }

        public void flush() throws IOException {
            synchronized (p.this) {
                p.this.b();
            }
            while (this.a.b > 0) {
                a(false);
                p.this.d.flush();
            }
        }
    }

    /* compiled from: Http2Stream */
    public final class b implements z {
        public final e a = new e();
        public final e b = new e();
        public final long c;
        public boolean d;
        public boolean e;

        public b(long j) {
            this.c = j;
        }

        public a0 B() {
            return p.this.i;
        }

        public long c(e eVar, long j) throws IOException {
            if (j >= 0) {
                b bVar;
                long c;
                loop0:
                while (true) {
                    bVar = null;
                    synchronized (p.this) {
                        p.this.i.i();
                        try {
                            p pVar = p.this;
                            b bVar2 = pVar.k;
                            if (bVar2 != null) {
                                bVar = bVar2;
                            }
                            if (this.d) {
                                throw new IOException("stream closed");
                            }
                            if (!pVar.e.isEmpty()) {
                                Objects.requireNonNull(p.this);
                            }
                            e eVar2 = this.b;
                            long j2 = eVar2.b;
                            if (j2 > 0) {
                                c = eVar2.c(eVar, Math.min(j, j2));
                                p pVar2 = p.this;
                                long j3 = pVar2.a + c;
                                pVar2.a = j3;
                                if (bVar == null && j3 >= ((long) (pVar2.d.n.a() / 2))) {
                                    pVar2 = p.this;
                                    pVar2.d.h0(pVar2.c, pVar2.a);
                                    p.this.a = 0;
                                }
                            } else if (this.e || bVar != null) {
                                c = -1;
                            } else {
                                p.this.j();
                            }
                        } finally {
                            j = p.this.i;
                            j.n();
                        }
                    }
                }
                p.this.i.n();
                if (c != -1) {
                    p.this.d.u(c);
                    return c;
                } else if (bVar == null) {
                    return -1;
                } else {
                    throw new u(bVar);
                }
            }
            throw new IllegalArgumentException(c.b.a.a.a.B("byteCount < 0: ", j));
        }

        public void close() throws IOException {
            long j;
            synchronized (p.this) {
                this.d = true;
                e eVar = this.b;
                j = eVar.b;
                eVar.n();
                if (!p.this.e.isEmpty()) {
                    Objects.requireNonNull(p.this);
                }
                p.this.notifyAll();
            }
            if (j > 0) {
                p.this.d.u(j);
            }
            p.this.a();
        }
    }

    /* compiled from: Http2Stream */
    public class c extends g.c {
        public IOException l(IOException iOException) {
            SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout");
            if (iOException != null) {
                socketTimeoutException.initCause(iOException);
            }
            return socketTimeoutException;
        }

        public void m() {
            p.this.e(b.CANCEL);
        }

        public void n() throws IOException {
            if (k()) {
                throw l(null);
            }
        }
    }

    public p(int i, g gVar, boolean z, boolean z2, @Nullable q qVar) {
        ArrayDeque arrayDeque = new ArrayDeque();
        this.e = arrayDeque;
        this.i = new c();
        this.j = new c();
        this.k = null;
        Objects.requireNonNull(gVar, "connection == null");
        this.c = i;
        this.d = gVar;
        this.b = (long) gVar.o.a();
        b bVar = new b((long) gVar.n.a());
        this.g = bVar;
        a aVar = new a();
        this.h = aVar;
        bVar.e = z2;
        aVar.c = z;
        if (qVar != null) {
            arrayDeque.add(qVar);
        }
        if (g() && qVar != null) {
            throw new IllegalStateException("locally-initiated streams shouldn't have headers yet");
        } else if (!g() && qVar == null) {
            throw new IllegalStateException("remotely-initiated streams should have headers");
        }
    }

    public void a() throws IOException {
        Object obj;
        boolean h;
        synchronized (this) {
            b bVar = this.g;
            if (!bVar.e && bVar.d) {
                a aVar = this.h;
                if (aVar.c || aVar.b) {
                    obj = 1;
                    h = h();
                }
            }
            obj = null;
            h = h();
        }
        if (obj != null) {
            c(b.CANCEL);
        } else if (!h) {
            this.d.s(this.c);
        }
    }

    public void b() throws IOException {
        a aVar = this.h;
        if (aVar.b) {
            throw new IOException("stream closed");
        } else if (aVar.c) {
            throw new IOException("stream finished");
        } else if (this.k != null) {
            throw new u(this.k);
        }
    }

    public void c(b bVar) throws IOException {
        if (d(bVar)) {
            g gVar = this.d;
            gVar.r.s(this.c, bVar);
        }
    }

    public final boolean d(b bVar) {
        synchronized (this) {
            if (this.k != null) {
                return false;
            } else if (this.g.e && this.h.c) {
                return false;
            } else {
                this.k = bVar;
                notifyAll();
                this.d.s(this.c);
                return true;
            }
        }
    }

    public void e(b bVar) {
        if (d(bVar)) {
            this.d.w(this.c, bVar);
        }
    }

    public y f() {
        synchronized (this) {
            if (!this.f) {
                if (!g()) {
                    throw new IllegalStateException("reply before requesting the sink");
                }
            }
        }
        return this.h;
    }

    public boolean g() {
        if (this.d.a == ((this.c & 1) == 1)) {
            return true;
        }
        return false;
    }

    public synchronized boolean h() {
        if (this.k != null) {
            return false;
        }
        b bVar = this.g;
        if (bVar.e || bVar.d) {
            a aVar = this.h;
            if ((aVar.c || aVar.b) && this.f) {
                return false;
            }
        }
        return true;
    }

    public void i() {
        boolean h;
        synchronized (this) {
            this.g.e = true;
            h = h();
            notifyAll();
        }
        if (!h) {
            this.d.s(this.c);
        }
    }

    public void j() throws InterruptedIOException {
        try {
            wait();
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            throw new InterruptedIOException();
        }
    }
}
